package com.sztx.pay.center.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class HttpClientUtil extends com.sztx.se.common.util.http.HttpClientUtil {
	protected static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);

	public static String post(String url, String data, String charSet) throws Exception {
		String responseStr = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);

		try {
			StringEntity uefEntity = new StringEntity(data, charSet);
			httpPost.setEntity(uefEntity);
			CloseableHttpResponse response = httpClient.execute(httpPost);

			try {
				HttpEntity entity = response.getEntity();

				if (entity != null) {
					responseStr = EntityUtils.toString(entity, charSet);
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			log.error("Http post request error", e);
		} catch (UnsupportedEncodingException e) {
			log.error("Http post request error", e);
		} catch (IOException e) {
			log.error("Http post request error", e);
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				log.error("Httpclient close error", e);
			}
		}

		return responseStr;
	}
	
	
	
	
}
